﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CellarDoor.Gui;
using CellarDoor.Gui.Elements;
using CellarDoor.Gui.Animation;
using CellarDoor.Gui.Controller;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CellarDoor.Gui.Demo
{
    public class DemoFrame : UiFrame
    {
        public DemoFrame(UiManager manager)
            : base(manager)
        {
            Initialize();
        }

        private UiFrame currentDemo;

        private void Initialize()
        {
            this.ElementName = "Frame";
            this.Padding = new UiPadding(26, 5, 5, 5);

            var strip = new UiMenuStrip(this);
            strip.FontName = "segoe";

            var item = strip.AddMenuItem("Demo");
            item.SubMenu.AddMenuItem("Basics").Click += (s, e) =>
            {
                if (currentDemo != null)
                {
                    currentDemo.Close();
                }
                currentDemo = new DemoScreens.ElementsScreen(Manager);
                currentDemo.Show();
            };
            item.SubMenu.AddMenuItem("XBOX Dash").Click += (s, e) =>
            {
                if (currentDemo != null)
                {
                    currentDemo.Close();
                }
                currentDemo = new Xbox.Dashboard(Manager);
                currentDemo.Show();
            };
            item.SubMenu.AddMenuItem("Animation").Click += (s, e) =>
            {
                if (currentDemo != null)
                {
                    currentDemo.Close();
                }
                currentDemo = new DemoScreens.AnimationScreen(Manager);
                currentDemo.Show();
            };
            item.SubMenu.AddMenuItem("Docking").Click += (s, e) =>
            {
                if (currentDemo != null)
                {
                    currentDemo.Close();
                }
                currentDemo = new DemoScreens.DockScreen(Manager);
                currentDemo.Show();
            };
            item.SubMenu.AddMenuItem("Event bubbling").Click += (s, e) =>
            {
                if (currentDemo != null)
                {
                    currentDemo.Close();
                }
                currentDemo = new DemoScreens.EventScreen(Manager);
                currentDemo.Show();
            };
            item.SubMenu.AddMenuItem("Exit").Click += (s, e) => { this.Manager.Game.Exit(); };
            item.SubMenu.Height = 200;
            item.SubMenu.Width = 150;

            item = strip.AddMenuItem("View");
            item.SubMenu.Height = 200;
            item.SubMenu.AddMenuItem("Debug").Click += (s, e) => { this.Manager.DebugEnable = !this.Manager.DebugEnable; };
            item.SubMenu.AddMenuItem("800x600").Click += (s, e) => { SwitchResolution(800, 600); };
            item.SubMenu.AddMenuItem("1024x768").Click += (s, e) => { SwitchResolution(1024, 768); };
            item.SubMenu.AddMenuItem("1024x800").Click += (s, e) => { SwitchResolution(1024, 800); };
            item.SubMenu.AddMenuItem("1280x800").Click += (s, e) => { SwitchResolution(1280, 800); };
            item.SubMenu.AddMenuItem("1280x960").Click += (s, e) => { SwitchResolution(1280, 960); };

            item = strip.AddMenuItem("About");
            item.SubMenu.AddMenuItem("CellarDoor UI").Click += (s, e) => { ; };
            item.SubMenu.Height = 100;
            item.SubMenu.Width = 200;
        }

        private void SwitchResolution(int width, int height)
        {
            var device = Manager.Game.Services.GetService(typeof(IGraphicsDeviceManager)) as GraphicsDeviceManager;
            device.PreferredBackBufferWidth = width;
            device.PreferredBackBufferHeight = height;
            device.ApplyChanges();
            Manager.Viewport = device.GraphicsDevice.Viewport;
        }
    }
}
